import Cookies from 'js-cookie'

interface IApp {
  sidebar: {
    opened: boolean
    withoutAnimation: boolean
    hide: boolean
  }
  device: 'desktop' | 'mobile'
}

const useAppStore = defineStore('app', {
  state: (): IApp => ({
    sidebar: {
      opened: Cookies.get('sidebarStatus') ? !!+(Cookies.get('sidebarStatus') as string) : true,
      withoutAnimation: false,
      hide: false,
    },
    device: 'desktop',
  }),
  actions: {
    toggleSideBar(withoutAnimation: boolean = false) {
      if (this.sidebar.hide) {
        return false
      }
      this.sidebar.opened = !this.sidebar.opened
      this.sidebar.withoutAnimation = withoutAnimation
      if (this.sidebar.opened) {
        Cookies.set('sidebarStatus', '1')
      } else {
        Cookies.set('sidebarStatus', '0')
      }
    },
    closeSideBar({ withoutAnimation }: { withoutAnimation: boolean }) {
      Cookies.set('sidebarStatus', '0')
      this.sidebar.opened = false
      this.sidebar.withoutAnimation = withoutAnimation
    },
    toggleDevice(device: IApp['device']) {
      this.device = device
    },
    toggleSideBarHide(status: boolean) {
      this.sidebar.hide = status
    },
  },
})

export default useAppStore
